﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , gt TECH, Ltd.
//--------------------------------------------------------------------

using System.Collections.Generic;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.Utilities;

     /// <summary>
     /// PiRoleManager
     /// 系统角色表
     ///
     /// 修改纪录
     ///
     ///		2012-03-02 版本：1.0 EricHu 创建主键。
     ///
     /// 版本：1.0
     ///
     /// <author>
     ///		<name>EricHu</name>
     ///		<date>2012-03-02</date>
     /// </author>
     /// </summary>
     public partial class PiRoleManager : DbCommonManager, IDbCommonManager
     {
      /// <summary>
      /// 构造函数
      /// </summary>
      public PiRoleManager()
      {
          base.CurrentTableName = PiRoleTable.TableName;
          base.PrimaryKey = "Id";
      }

      /// <summary>
      /// 构造函数
      /// <param name="tableName">指定表名</param>
      /// </summary>
      public PiRoleManager(string tableName)
      {
          base.CurrentTableName = tableName;
      }

      /// <summary>
      /// 构造函数
      /// </summary>
      /// <param name="dbProvider">数据库连接</param>
      public PiRoleManager(IDbProvider dbProvider): this()
      {
          DBProvider = dbProvider;
      }

      /// <summary>
      /// 构造函数
      /// </summary>
      /// <param name="userInfo">用户信息</param>
      public PiRoleManager(UserInfo userInfo) : this()
      {
          UserInfo = userInfo;
      }

      /// <summary>
      /// 构造函数
      /// </summary>
      /// <param name="dbProvider">数据库连接</param>
      /// <param name="userInfo">用户信息</param>
      public PiRoleManager(IDbProvider dbProvider, UserInfo userInfo) : this(dbProvider)
      {
          UserInfo = userInfo;
      }

      /// <summary>
      /// 构造函数
      /// </summary>
      /// <param name="dbProvider">数据库连接</param>
      /// <param name="userInfo">用户信息</param>
      /// <param name="tableName">指定表名</param>
      public PiRoleManager(IDbProvider dbProvider, UserInfo userInfo, string tableName) : this(dbProvider, userInfo)
      {
          base.CurrentTableName = tableName;
      }

      /// <summary>
      /// 添加
      /// </summary>
      /// <param name="piRoleEntity">实体</param>
      /// <returns>主键</returns>
      public string Add(PiRoleEntity piRoleEntity)
      {
          return this.AddEntity(piRoleEntity);
      }

      /// <summary>
      /// 添加
      /// </summary>
      /// <param name="piRoleEntity">实体</param>
      /// <param name="identity">自增量方式</param>
      /// <param name="returnId">返回主键</param>
      /// <returns>主键</returns>
      public string Add(PiRoleEntity piRoleEntity, bool identity, bool returnId)
      {
          this.Identity = identity;
          this.ReturnId = returnId;
          return this.AddEntity(piRoleEntity);
      }

      /// <summary>
      /// 更新
      /// </summary>
      /// <param name="piRoleEntity">实体</param>
      public int Update(PiRoleEntity piRoleEntity)
      {
          return this.UpdateEntity(piRoleEntity);
      }

      /// <summary>
      /// 获取实体
      /// </summary>
      /// <param name="id">主键</param>
      public PiRoleEntity GetEntity(string id)
      {
          return GetEntity(int.Parse(id));
      }

      public PiRoleEntity GetEntity(int id)
      {
          PiRoleEntity piRoleEntity = new PiRoleEntity(this.GetDT(PiRoleTable.FieldId, id));
          return piRoleEntity;
      }

      /// <summary>
      /// 添加实体
      /// </summary>
      /// <param name="piRoleEntity">实体</param>
      public int AddEntity(PiRoleEntity piRoleEntity, out string statusCode)
      {
          int returnValue = 0;
          string sequence = string.Empty;
          statusCode = string.Empty;
          if (piRoleEntity.SortCode == null || piRoleEntity.SortCode == 0)
          {
              CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
              sequence = sequenceManager.GetSequence(this.CurrentTableName);
              piRoleEntity.SortCode = int.Parse(sequence);
          }
           SQLBuilder sqlBuilder = new SQLBuilder(DBProvider, this.Identity, this.ReturnId);
           sqlBuilder.BeginInsert(this.CurrentTableName, PiRoleTable.FieldId);
           if (!this.Identity)
           {
               sqlBuilder.SetValue(PiRoleTable.FieldId, piRoleEntity.Id);
           }
           else
           {
               if (!this.ReturnId && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
               {
                   if (DBProvider.CurrentDbType == CurrentDbType.Oracle)
                   {
                       sqlBuilder.SetFormula(PiRoleTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
                   }
                   if (DBProvider.CurrentDbType == CurrentDbType.DB2)
                   {
                       sqlBuilder.SetFormula(PiRoleTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
                   }
               }
               else
               {
                   if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                   {
                       if (piRoleEntity.Id == null)
                       {
                           if (string.IsNullOrEmpty(sequence))
                           {
                               CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                               sequence = sequenceManager.GetSequence(this.CurrentTableName);
                           }
                           piRoleEntity.Id = int.Parse(sequence);
                       }
                       sqlBuilder.SetValue(PiRoleTable.FieldId, piRoleEntity.Id);
                   }
               }
           }
           this.SetEntity(sqlBuilder, piRoleEntity);
           if (UserInfo != null)
           {
               sqlBuilder.SetValue(PiRoleTable.FieldCreateUserId, UserInfo.Id);
               sqlBuilder.SetValue(PiRoleTable.FieldCreateBy, UserInfo.RealName);
           }
           sqlBuilder.SetDBNow(PiRoleTable.FieldCreateOn);
           if (UserInfo != null)
           {
               sqlBuilder.SetValue(PiRoleTable.FieldModifyUserId, UserInfo.Id);
               sqlBuilder.SetValue(PiRoleTable.FieldModifiedBy, UserInfo.RealName);
           }
           sqlBuilder.SetDBNow(PiRoleTable.FieldModifiedOn);
           if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.SqlServer || DBProvider.CurrentDbType == CurrentDbType.Access))
           {
               returnValue = sqlBuilder.EndInsert();
               // 运行成功
               statusCode = StatusCode.OKAdd.ToString();
           }
           else
           {
               returnValue = sqlBuilder.EndInsert();
           }
           return returnValue;
      }

      /// <summary>
      /// 更新实体
      /// </summary>
      /// <param name="piRoleEntity">实体</param>
      public int UpdateEntity(PiRoleEntity piRoleEntity, out string statusCode)
      {
          int returnValue = 0;
          SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
          sqlBuilder.BeginUpdate(this.CurrentTableName);
          this.SetEntity(sqlBuilder, piRoleEntity);
          if (UserInfo != null)
          {
              sqlBuilder.SetValue(PiRoleTable.FieldModifyUserId, UserInfo.Id);
              sqlBuilder.SetValue(PiRoleTable.FieldModifiedBy, UserInfo.RealName);
          }
          sqlBuilder.SetDBNow(PiRoleTable.FieldModifiedOn);
          sqlBuilder.SetWhere(PiRoleTable.FieldId, piRoleEntity.Id);
          returnValue = sqlBuilder.EndUpdate();
          if (returnValue == 1)
          {
              statusCode = StatusCode.OKUpdate.ToString();
          }
          else
          {
              statusCode = StatusCode.ErrorChanged.ToString();
          }

          return returnValue;
      }

      /// <summary>
      /// 更新实体
      /// </summary>
      /// <param name="baseRoleEntity">实体</param>
      public int UpdateEntity(PiRoleEntity roleEntity)
      {
          SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
          sqlBuilder.BeginUpdate(PiRoleTable.TableName);
          this.SetEntity(sqlBuilder, roleEntity);
          if (UserInfo != null)
          {
              sqlBuilder.SetValue(PiRoleTable.FieldModifyUserId, UserInfo.Id);
              sqlBuilder.SetValue(PiRoleTable.FieldModifiedBy, UserInfo.RealName);
          }
          sqlBuilder.SetDBNow(PiRoleTable.FieldModifiedOn);
          sqlBuilder.SetWhere(PiRoleTable.FieldId, roleEntity.Id);
          return sqlBuilder.EndUpdate();
      }

      /// <summary>
      /// 设置实体
      /// </summary>
      /// <param name="piRoleEntity">实体</param>
      private void SetEntity(SQLBuilder sqlBuilder, PiRoleEntity piRoleEntity)
      {
          sqlBuilder.SetValue(PiRoleTable.FieldSystemId, piRoleEntity.SystemId);
          sqlBuilder.SetValue(PiRoleTable.FieldOrganizeId, piRoleEntity.OrganizeId);
          sqlBuilder.SetValue(PiRoleTable.FieldCategory, piRoleEntity.Category);
          sqlBuilder.SetValue(PiRoleTable.FieldCode, piRoleEntity.Code);
          sqlBuilder.SetValue(PiRoleTable.FieldEnabled, piRoleEntity.Enabled);
          sqlBuilder.SetValue(PiRoleTable.FieldRealName, piRoleEntity.RealName);
          sqlBuilder.SetValue(PiRoleTable.FieldAllowEdit, piRoleEntity.AllowEdit);
          sqlBuilder.SetValue(PiRoleTable.FieldAllowDelete, piRoleEntity.AllowDelete);
          sqlBuilder.SetValue(PiRoleTable.FieldSortCode, piRoleEntity.SortCode);
          sqlBuilder.SetValue(PiRoleTable.FieldDescription, piRoleEntity.Description);
      }

      /// <summary>
      /// 删除实体
      /// </summary>
      /// <param name="id">主键</param>
      /// <returns>影响行数</returns>
      public int Delete(string id)
      {
          int returnValue = 0;
          // 删除角色权限结构定义
          // returnValue = DbCommonLibary.Delete(DBProvider, BaseRoleModuleOperationTable.TableName, BaseRoleModuleOperationTable.FieldRoleId, id);
          // 删除员工角色表结构定义部分
          returnValue += DbCommonLibary.Delete(DBProvider, PiUserRoleTable.TableName, PiUserRoleTable.FieldRoleId, id);
          // 删除角色的表结构定义部分
          returnValue += DbCommonLibary.Delete(DBProvider, PiRoleTable.TableName, id);
          return returnValue;
      }

      #region public int BatchDelete(string id) 批量删除
      /// <summary>
      /// 批量删除
      /// </summary>
      /// <param name="id">主键数组</param>
      /// <returns>影响行数</returns>
      public int BatchDelete(string[] ids)
      {
          int returnValue = 0;
          for (int i = 0; i < ids.Length; i++)
          {
              returnValue += this.Delete(ids[i]);
          }
          return returnValue;
      }
      #endregion

      #region public int BatchSave(List<BaseRoleEntity> roleEntites) 批量保存
      /// <summary>
      /// 批量保存
      /// </summary>
      /// <param name="dataTable">数据表</param>
      /// <returns>影响行数</returns>
      public int BatchSave(List<PiRoleEntity> roleEntites)
      {
          /*
          foreach (BaseRoleEntity roleEntity in roleEntites)
          {
              // 删除状态
              if (dataRow.RowState == DataRowState.Deleted)
              {
                  string id = dataRow[BaseRoleEntity.FieldId, DataRowVersion.Original].ToString();
                  if (id.Length > 0)
                  {
                      returnValue += this.Delete(id);
                  }
              }
              // 被修改过
              if (dataRow.RowState == DataRowState.Modified)
              {
                  string id = dataRow[BaseRoleEntity.FieldId, DataRowVersion.Original].ToString();
                  if (!String.IsNullOrEmpty(id))
                  {
                      roleEntity.GetFrom(dataRow);
                      returnValue += this.UpdateEntity(roleEntity);
                  }
              }
              // 添加状态
              if (dataRow.RowState == DataRowState.Added)
              {
                  roleEntity.GetFrom(dataRow);
                  returnValue += this.AddEntity(roleEntity).Length > 0 ? 1 : 0;
              }
              if (dataRow.RowState == DataRowState.Unchanged)
              {
                  continue;
              }
              if (dataRow.RowState == DataRowState.Detached)
              {
                  continue;
              }
          }
          */

          int returnValue = 0;
          foreach (PiRoleEntity roleEntity in roleEntites)
          {
              returnValue += this.UpdateEntity(roleEntity);
          }
          return returnValue;
      }
      #endregion
  }
}
